package org.third.integration.hibernate;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import org.hibernate.mapping.Map;
//
//class JacksonAuthConfigDeserializer extends JsonDeserializer<JacksonAuthConfig> {
//
//    @Override
//    public JacksonAuthConfig deserialize(JsonParser jp, DeserializationContext ctxt)
//            throws IOException, JsonProcessingException {
//        return null;
//    }
//
//}

//class JacksonAuthConfigSerializer extends JsonSerializer<Map<String, String>> {
//
//    @Override
//    public void serialize(Map<String, String> value, JsonGenerator jgen, SerializerProvider provider)
//            throws IOException, JsonProcessingException {
//
//        for (Map.Entry<String, String> entry : value.entrySet()) {
//            jgen.writeStartObject();
//            jgen.writeStartObject();
//
//            jgen.writeEndObject();
//            jgen.writeEndObject();
//        }
//    }
//
//}


@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@JsonPropertyOrder(value = { "id", "name", "displayName", "metadata" })
class JacksonAuthConfig {
    private String id;
    private String name;
    // @JsonDeserialize(using = JacksonAuthConfigDeserializer.class)
    // @JsonSerialize(using = JacksonAuthConfigSerializer.class)
    private Set<AbstractUserMetadata> metadata = new LinkedHashSet<>();

    /**
     * @return the id of JacksonAuthConfig
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the name of JacksonAuthConfig
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the metadata of JacksonAuthConfig
     */
    public Set<AbstractUserMetadata> getMetadata() {
        if (null == metadata) {
            metadata = new HashSet<AbstractUserMetadata>();
        }
        return metadata;
    }

    /**
     * @param metadata the metadata to set
     */
    public void setMetadata(Set<AbstractUserMetadata> metadata) {
        this.metadata = metadata;
    }

}

public class JacksonTest {
    public static void main(String[] args) {

        // JacksonAuthConfig config = new JacksonAuthConfig();
        // config.setId("123");
        // config.setName("hello123");
        // config.getMetadata().add(new AbstractUserMetadata("firstName", "zhang"));
        // config.getMetadata().add(new AbstractUserMetadata("secondName", "qiang"));
        //
        // config.getMetadata().add(new AbstractUserMetadata("groups", "UserGroup"));
        // config.getMetadata().add(new AbstractUserMetadata("groups", "AdminGroup"));
        // config.getMetadata().add(new AbstractUserMetadata("groups", "RoleGroup"));
//        AbstractUser abstractUser = new AbstractUser();
//        abstractUser.setId("123");
//        abstractUser.setName("hello123");
//        abstractUser.getMetadata().add(new AbstractUserMetadata("firstName", "zhang"));
//        abstractUser.getMetadata().add(new AbstractUserMetadata("secondName", "qiang"));
//
//        abstractUser.getMetadata().add(new AbstractUserMetadata("groups", "UserGroup"));
//        abstractUser.getMetadata().add(new AbstractUserMetadata("groups", "AdminGroup"));
//        abstractUser.getMetadata().add(new AbstractUserMetadata("groups", "RoleGroup"));
//
//        User u = new User();
//        u.setId("123");
//        Map<String, String> profile = new HashMap<String, String>();
//        profile.put("group", "1b");
//        profile.put("group(2)", "1b");
//        u.setProfile(profile);
//
//        HibernateAwareObjectMapper mapper = new HibernateAwareObjectMapper();
//        String json = "";
//        try {
//            json = mapper.writeValueAsString(abstractUser);
//        } catch (JsonProcessingException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        System.out.println(json);

    }

}
